package com.videomeetting.legend.videomeetting.activity.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.videomeetting.legend.videomeetting.R;
import com.videomeetting.legend.videomeetting.activity.bean.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by legend on 18-6-29.
 */

public class OnliveFragment extends Fragment implements View.OnClickListener {
    Button saveBtn;
    EditText trspEt;
    ImageView trspIv;
    EditText udpEt;
    ImageView udppIv;
    EditText rtmpEt;
    ImageView rtmpIv;
    SharedPreferences sharedPreferences;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_onlive, container, false);
        sharedPreferences = getActivity().getSharedPreferences("onlive", Context.MODE_PRIVATE);
        String rtsp = sharedPreferences.getString("rtsp", "");
        String udp = sharedPreferences.getString("udp", "");
        String rtmp = sharedPreferences.getString("rtmp", "");

        saveBtn = (Button) view.findViewById(R.id.fragment_onlive_save_btn);
         trspEt = (EditText) view.findViewById(R.id.fragment_onlive_rtsp_et);
         trspIv = (ImageView) view.findViewById(R.id.fragment_onlive_rtsp_iv);

         udpEt = (EditText) view.findViewById(R.id.fragment_onlive_udp_et);
         udppIv = (ImageView) view.findViewById(R.id.fragment_onlive_udp_iv);

         rtmpEt = (EditText) view.findViewById(R.id.fragment_onlive_rtmp_et);
         rtmpIv = (ImageView) view.findViewById(R.id.fragment_onlive_rtmp_iv);
        if (!TextUtils.isEmpty(rtsp)){
            trspEt.setText(rtsp);
        }
        if (!TextUtils.isEmpty(udp)){
            udpEt.setText(udp);
        }
        if (!TextUtils.isEmpty(rtmp)){
            rtmpEt.setText(rtmp);
        }
        saveBtn.setOnClickListener(this);
         trspIv.setOnClickListener(this);
        udppIv.setOnClickListener(this);
        rtmpIv.setOnClickListener(this);

        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent messageEvent) {
//        if (messageEvent.getCode()==5) {
//            Toast.makeText(getActivity(), messageEvent.getMessage(), Toast.LENGTH_SHORT);
////            filechooseEdit.setText(messageEvent.getMessage());
//        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //保存
            case R.id.fragment_onlive_save_btn:
                sharedPreferences = this.getActivity().getSharedPreferences("onlive", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putString("rtsp",trspEt.getText().toString().trim());
                edit.putString("udp",udpEt.getText().toString().trim());
                edit.putString("rtmp",rtmpEt.getText().toString().trim());
                edit.commit();
                break;
                //RTSP
            case R.id.fragment_onlive_rtsp_iv:
//                TextChooseFragment rtspFragment = new TextChooseFragment();
//                rtspFragment.show(getChildFragmentManager(),"rtsp");
                break;
                //UDP
            case R.id.fragment_onlive_udp_iv:
//                TextChooseFragment udpFragment = new TextChooseFragment();
//                udpFragment.show(getChildFragmentManager(),"udp");
                break;
                //RTMP
            case R.id.fragment_onlive_rtmp_iv:
//                TextChooseFragment rtmpFragment = new TextChooseFragment();
//                rtmpFragment.show(getChildFragmentManager(),"rtmp");
                break;
        }
    }
}
